package com.zhouyibike.uu.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.GetYiTransferResult;
import com.zhouyibike.uu.utils.TextUtils;

import java.util.List;

/**
 * Created by cc on 2017/5/3.
 */

public class YihuodongAdapter extends BaseAdapter {
    private Context context;
    private List<GetYiTransferResult.DataBean> list;

    public YihuodongAdapter(Context context, List<GetYiTransferResult.DataBean> list) {
        this.context = context;
        this.list = list;
    }
    public  void setList(List<GetYiTransferResult.DataBean> list){
        this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {// 如果是第一次显示该页面(要记得保存到viewholder中供下次直接从缓存中调用)
            convertView= LayoutInflater.from(context).inflate(R.layout.item_yihuodongjilu2,null);
            holder = new ViewHolder();
            // 以下为保存这一屏的内容，供下次回到这一屏的时候直接refresh，而不用重读布局文件
            holder.phone = (TextView) convertView.findViewById(R.id.yihuodongzhuanrangitem_phone);
            holder.tv_time = (TextView) convertView.findViewById(R.id.yihuodongzhuanrangitem_time);
            holder.buyinfo = (TextView)convertView.findViewById(R.id.yihuodongzhuanrangitem_buyinfo);
            holder.buyinco = (TextView)convertView.findViewById(R.id.yihuodongzhuanrangitem_isbuy);
            holder.img = (ImageView) convertView.findViewById(R.id.yihuodongzhuanrangitem_isbuyicon);
            convertView.setTag(holder);
        } else {// 如果之前已经显示过该页面，则用viewholder中的缓存直接刷屏
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_time.setText(TextUtils.toFormatPhone(list.get(position).getUserName())+"\n"+list.get(position).getCreateTime());
//        holder.phone.setText(TextUtils.toFormatPhone(list.get(position).getUserName()));
//        if (list.get(position).getIsBuy()==0)//0==转出 1==买入
//        {
//            holder.buyinfo.setText("转出\n"+list.get(position).getAmount()+"");
//        }else{
//            holder.buyinfo.setText("买入\n"+list.get(position).getAmount()+"");
//        }
        holder.buyinfo.setText(list.get(position).getAmount()+"");
        if (list.get(position).getIsBuy()==0)//0==转出 1==买入
        {
            holder.img.setImageDrawable(context.getResources().getDrawable(R.drawable.bgzhuanrang2));
            holder.buyinco.setText("转让");
        }else{
            holder.img.setImageDrawable(context.getResources().getDrawable(R.drawable.bggoumia2));
            holder.buyinco.setText("抢购中");
        }
        return convertView;
    }

    public final class ViewHolder {
        public ImageView img;
        public TextView phone;
        public TextView tv_time;
        public TextView buyinfo;
        public TextView buyinco;
    }
}
